package com.Lixiaoqian.CardPlay.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.Lixiaoqian.CardPlay.R;
import com.Lixiaoqian.CardPlay.utils.Constant;
import com.Lixiaoqian.CardPlay.utils.PrefUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GuiteActivity extends AppCompatActivity {


    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.btnentry)
    Button btnentry;
    private List<View> dates = new ArrayList<>();
    private int[] res = {R.mipmap.guide1, R.mipmap.guide2, R.mipmap.guide3, R.mipmap.guide4};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guite);
        ButterKnife.bind(this);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        initData();
        setDos(0);

        viewpager.setAdapter(pagerAdapter);

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setDos(position);
                if (position == 3) {
                    btnentry.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        for (int i = 0; i < res.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(res[i]);
            dates.add(imageView);
        }
    }


    public void setDos(int position) {
        setAllNomaldos();
        View childAt = ll.getChildAt(position);
        childAt.setSelected(true);
    }

    public void setAllNomaldos() {
        for (int i = 0; i < ll.getChildCount(); i++) {
            ll.getChildAt(i).setSelected(false);
        }
    }

    public void btn_Entry(View view) {
        startActivity(new Intent(this, MainActivity.class));
        PrefUtils.putBoolean(Constant.ISFAIRST_APP,false);
        finish();
    }

    public static void launch(Activity fromActivity){
        fromActivity.startActivity(new Intent(fromActivity,GuiteActivity.class));

    }

    PagerAdapter pagerAdapter=new PagerAdapter() {
        @Override
        public int getCount() {
            return dates.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(dates.get(position));
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(dates.get(position));
            return dates.get(position);
        }
    };
}
